package com.kehongborui.ticket.service.impl;

import com.kehongborui.ticket.entity.User;
import com.kehongborui.ticket.mapper.UserMapper;
import com.kehongborui.ticket.model.UserModel;
import com.kehongborui.ticket.service.LoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.kehongborui.ticket.util.CommonUtil.isEmpty;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-08-01
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    UserMapper userMapper;

    /**
     * 用户登录逻辑处理
     * @param model
     * @return
     */
    public UserModel login(UserModel model) {
        if (isEmpty(model.getPassword()) || isEmpty(model.getUsername())) {
            throw new RuntimeException("用户名或者密码不能为空");
        }

        // 查找用户
        User user = new User();
        BeanUtils.copyProperties(model, user);
        User retUser = userMapper.findByUsername(model.getUsername());

        if (retUser != null && retUser.getPassword().equals(model.getPassword())) {
            // 将 entity retUser 中的数据
            BeanUtils.copyProperties(retUser, model);
            return model;
        }

        return null;
    }
}
